package com.sinoglobal.ningxia.utils;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

/**
 * 功能: 自定义Dialog进行设置的工具类
 * 文件名: com.example.weishi.tool.utils
 * Created by XINDZ on 2016/6/28.
 */
public class DialogUtils {

    private static DialogUtils dInstance;
    private Activity activity;

    public DialogUtils(Activity activity) {
        this.activity = activity;
    }

    public static DialogUtils getInstance(Activity activity) {
        if (dInstance == null) {
            dInstance = new DialogUtils(activity);
        }
        return dInstance;
    }

    /**
     * 功能:
     *
     * @param view
     * @param dialog
     * @param i      显示高度为界面的 i%
     * @param layout dialog的显示动画 -1表示不设置
     * @author XINDZ
     * 2016/6/28  16:17
     */
    public boolean setDialog(View view, Dialog dialog, double i, int layout) {
        if (null != view || null != dialog) {
            try {

                dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Window window = dialog.getWindow();
                // 设置显示动画
                if (layout != -1) {
                    window.setWindowAnimations(layout);
                }
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.x = 0;
                wl.y = activity.getWindowManager().getDefaultDisplay().getHeight();
                // 以下这两句是为了保证按钮可以水平满屏
                wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
                wl.height = (int) (activity.getWindowManager().getDefaultDisplay().getHeight() * i); // 高度设置为屏幕的0.7
                // 设置显示位置
                dialog.onWindowAttributesChanged(wl);
                // 设置点击外围解散
                dialog.setCanceledOnTouchOutside(true);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;

        }
    }

    /**
     * 功能:
     *
     * @param view
     * @param dialog
     * @param layout dialog的显示动画 -1表示不设置
     * @author XINDZ
     * 2016/6/28  16:17
     */
    public boolean setDialog(View view, Dialog dialog, int layout) {
        if (null != view || null != dialog) {
            try {

                dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                Window window = dialog.getWindow();
                // 设置显示动画
                if (layout != -1) {
                    window.setWindowAnimations(layout);
                }
                window.setGravity(Gravity.BOTTOM);
                WindowManager.LayoutParams wl = window.getAttributes();
                wl.x = 0;
                wl.y = activity.getWindowManager().getDefaultDisplay().getHeight();
                // 以下这两句是为了保证按钮可以水平满屏
                wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
                wl.height = ViewGroup.LayoutParams.MATCH_PARENT; // 高度设置为屏幕的0.7
                // 设置显示位置
                dialog.onWindowAttributesChanged(wl);
                // 设置点击外围解散
                dialog.setCanceledOnTouchOutside(true);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        } else {
            return false;

        }
    }

}
